<template>
	<view class="content">
		<view class="row b-b">
			<text class="tit">联系人</text>
			<input class="input" type="text" v-model="addressData.name" placeholder="收货人姓名" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<text class="tit">电话号码</text>
			<input class="input" type="number" v-model="addressData.mobile" placeholder="收货人手机号码" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<view class="tit">
				所在地区
			</view>
			<view class="input" @tap="chooseCity">
				{{addressData.region.label}}
			</view>

		</view>
		<view class="row b-b">
			<text class="tit">地址</text>
			<text @click="chooseLocation" class="input">
				{{addressData.addressName}}
			</text>
			<text class="yticon icon-shouhuodizhi"></text>
		</view>

		<view class="row b-b">
			<text class="tit">详情地址</text>
			<!-- <input class="input" type="text" v-model="addressData.area" placeholder="楼号、门牌" placeholder-class="placeholder" /> -->
			<textarea class="input" v-model="addressData.street1" auto-height="true" placeholder="输入详细地址" placeholder-class="placeholder"></textarea>
		</view>

		<view class="row default-row">
			<text class="tit">设为默认</text>
			<switch :checked="addressData.isDefault" color="#FFBC21" @change="switchChange" />
		</view>
		<button class="add-btn" @click="save">提交</button>
		<mpvue-city-picker :themeColor="addressData.themeColor" ref="mpvueCityPicker" :pickerValueDefault="cityPickerValue"
		 @onCancel="onCancel" @onConfirm="onConfirm"></mpvue-city-picker>

	</view>
</template>

<script>
	import Api from '@/common/api';
	import mpvueCityPicker from '@/components/mpvue-citypicker/mpvueCityPicker.vue'
	export default {
		components: {
			mpvueCityPicker
		},
		data() {
			return {
				cityPickerValue: [0, 0, 1],
				editType:'edit',
				addressData: {
					// name: '',
					// mobile: '',
					addressName: '在地图选择',
					address: '',
					// detailed: '',
					// default: false,
					// cityPickerValue: [0, 0, 1],
					// themeColor: '#007AFF',
					// region: {
					// 	label: "请点击选择地址",
					// 	value: [],
					// 	cityCode: ""
					// }
					id: 0,
					email: "",
					name: "",
					telephone: "",
					company: "",
					street1: "",
					street2: "",
					city: "",
					province: "",
					district: "",
					country: "",
					user_id: '',
					is_default: '',
					region: {
						label: '请点击选择地址',
						cityValue: [],
						cityCode: ""
					},
					isDefault: false
				}
			}
		},
		onLoad(option) {
			let title = '新增收货地址';
			this.editType = option.type;
			if (option.type === 'edit') {
				title = '编辑收货地址'
				// this.addressData = JSON.parse(option.data)
				uni.getStorage({
					key: 'address',
					success: (e) => {
						this.addressData = e.data;
						this.addressData.addressName = '在地图选择';
						this.addressData.address = '',
						this.cityPickerValue = e.data.region.value;
						// this.addressData.name = e.data.name;
						// // this.addressData.mobile = e.data.tel;
						// this.addressData.mobile = '13554762756';
						// this.addressData.detailed = e.data.address.detailed;
						// this.addressData.isDefault = e.data.isDefault;
						// this.addressData.cityPickerValue = e.data.address.region.value;
						// this.addressData.region = e.data.address.region;
					}
				})

			}
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			switchChange(e) {
				this.addressData.isDefault = true;
			},
			//地图选择地址
			chooseLocation(){
				uni.chooseLocation({
					success: (data)=> {
						this.addressData.addressName = data.name;
						this.addressData.address = data.name;
						this.$forceUpdate();
					}
				})
			},
			chooseCity() {
				this.$refs.mpvueCityPicker.show()
			},
			onConfirm(e) {
				this.addressData.region = e;
				this.cityPickerValue = e.value;
				var array = e.label.split('-');
				this.addressData.province = array[0];
				this.addressData.city = array[1];
				this.addressData.district = array[2];
				this.addressData.cityValue = e.value.toString();
				this.addressData.cityCode = e.cityCode;
			},
			onCancel(e) {
				console.log(e)
			},
			//提交
			async save() {
				let data = this.addressData;
				if (!data.name) {
					this.$api.msg('请填写收货人姓名');
					return;
				}
				if (!/(^1[3|4|5|7|8][0-9]{9}$)/.test(data.mobile)) {
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if (data.region.value.length == 0) {
					uni.showToast({
						title: '请选择收件地址',
						icon: 'none'
					});
					return;
				}
				// if (!data.address) {
				// 	this.$api.msg('请在地图选择所在位置');
				// 	return;
				// }
				if (!data.street1) {
					uni.showToast({
						title: '请输入收件人详细地址',
						icon: 'none'
					});
					return;
				}
				let params = {
					'id': this.addressData.id,
					'email': this.addressData.email,
					'name': this.addressData.name,
					'telephone': this.addressData.mobile,
					'company': this.addressData.company,
					'street1': this.addressData.street1,
					'city': this.addressData.city,
					'province': this.addressData.province,
					'district': this.addressData.district,
					'country': this.addressData.country,
					'cityValue': this.addressData.cityValue,
					'cityCode': this.addressData.cityCode,
					'is_default': this.addressData.isDefault == true ? 1 : 0,
				};
				let result;
				if (this.editType === 'edit')
				 {
					//编辑
					 result = await Api.apiCall('put', Api.goods.address, params);
				 }
				 else
				 {
					 //新增
					 result = await Api.apiCall('post', Api.goods.address, params);
					
				 }
				if (result) {
					uni.navigateBack()
					// uni.redirectTo({
					// 	url: '/pages/user/address'
					// })
				}


			}

		},
		onBackPress() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel();
				return true;
			}
		},
		onUnload() {
			if (this.$refs.mpvueCityPicker.showPicker) {
				this.$refs.mpvueCityPicker.pickerCancel()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-top: 16upx;
	}

	.row {
		display: flex;
		align-items: center;
		position: relative;
		padding: 0 30upx;
		height: 110upx;
		background: #fff;

		.tit {
			flex-shrink: 0;
			// width: 120upx;
			width: 25%;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.input {
			flex: 1;
			width: 75%;
			font-size: 30upx;
			color: $font-color-dark;
		}

		.icon-shouhuodizhi {
			font-size: 36upx;
			color: $font-color-light;
		}
	}

	.default-row {
		margin-top: 16upx;

		.tit {
			flex: 1;
		}

		switch {
			transform: translateX(16upx) scale(.9);
		}
	}

	.add-btn {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		// background-color: $base-color;
		background-color: #FFBC21;
		border-radius: 10upx;
		// box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
		box-shadow: 1px 2px 5px rgba(255, 188, 33, 0.4);
	}
</style>
